apiready=function () {

  window.closeFrame = function() {
      api.closeFrame();
      api.sendEvent({
          name: 'closePublish',
          extra: {
              sta_close: 1
          }
      });

  };
  window.submit = function() {
      var url = "http://d.apicloud.com/mcm/api/file";
      var url1 = 'http://www.mk517.com/webApi/publishPost.php';
      var post_content = $api.byId('post_content');
      var token = $api.getStorage('token');

      if (imgSrc == '' && post_content.value == '') {
          api.alert({
              msg: '您还没有输入内容或者图片。。。。',
          }, function(ret, err) {});
      } else {
          if (imgSrc != '' && post_content.value == '') {
              uploadImage(imgSrc, url);
          };
          if (imgSrc != '' && post_content.value != '') {
              uploadImage(imgSrc, url, post_content.value);
          }

          if (post_content.value != '' && imgSrc == '') {
              var body = {
                  token: token,
                  post_content: post_content.value
              };
              publishCon(body);
          };
      }
  };

  /*上传图片*/
  window.choice_uploadImage = function() {
      choiceImage();
  }

  function publishCon(body) {
      api.ajax({
          url: 'http://www.mk517.com/webApi/publishPost.php',
          method: 'post',
          dataType: 'json',
          "headers": {
              "Content-Type": "application/json"
          },
          data: {
              body: body
          }
      }, function(ret, err) {
          if (ret) {
              if (ret.info == '发表成功') {
                  api.alert({
                      msg: '发表成功',
                  }, function(ret, err) {
                      if (ret) {
                          var index = '';
                          if (ret.buttonIndex == 1) {
                              api.closeFrame();
                              api.sendEvent({
                                  name: 'publish',
                                  extra: {
                                      index: 2
                                  }
                              });
                          }
                      } else {
                          // alert(JSON.stringify(err));
                      }
                  });

              } else {
                  api.alert({
                      msg: '发表失败！请重试'
                  }, function(ret, err) {});

              }
          } else {
              //alert(JSON.stringify(err));
          }
      });
  }

  /*选择图片功能*/
  imgSrc = '';

  function choiceImage() {
      api.actionSheet({
          cancelTitle: '取消',
          buttons: ['拍照', '手机相册']
      }, function(ret, err) {
          if (ret) {
              getPicture(ret.buttonIndex);
          } else {
              alert( JSON.stringify( err ) );
          }
      });

  };

  function getPicture(index) {
      if (index == 1) {
          api.getPicture({
              sourceType: 'camera',
              encodingType: 'jpg',
              mediaValue: 'pic',
              destinationType: 'url',
              allowEdit: true,
              quality: 50,
              targetWidth: 100,
              targetHeight: 100,
              saveToPhotoAlbum: false
          }, function(ret, err) {
              if (ret) {
                  imgSrc = ret.data;
                  // var url="http://d.apicloud.com/mcm/api/contentFile";
                  // var url1='http://www.mk517.com/webApi/publishPost.php';
                  if (imgSrc != '') {
                      var ele = $api.dom('#content_image');
                      $api.byId('img_wrap').style.display = 'block';
                      $api.attr(ele, 'src', imgSrc);
                  }
                  //alert(JSON.stringify(ret));
              } else {
                  // alert(JSON.stringify(err));
              }
          });

      } else if (index == 2) {
          var image = api.require('UIMediaScanner');
          image.open({
              type: 'picture',
              column: 4,
              classify: false,
              max: 1,
              sort: {
                  key: 'time',
                  order: 'desc'
              },
              texts: {
                  stateText: '已选择*项',
                  cancelText: '取消',
                  finishText: '完成'
              },
              styles: {
                  bg: '#afa',
                  mark: {
                      icon: '',
                      position: 'top_right',
                      size: 20
                  }
              }
          }, function(ret) {
              if (ret) {
                  imgSrc = ret.list[0].path;
                  if (ret.eventType == "confirm") {
                      var ele = $api.dom('#content_image');
                      $api.byId('img_wrap').style.display = 'block';
                      $api.attr(ele, 'src', imgSrc);
                  }
                  //  alert( JSON.stringify( ret ) );
              }
          });

      }
  }

  /* 上传图片函数 */
  function uploadImage(imageSrc, url, content) {
      var now = Date.now();
      var appId = 'A6054039139943';
      var appKey = SHA1("A6054039139943" + "UZ" + "5C8451CE-5E66-0FAF-B7D9-FDAF77F817FC" + "UZ" + now) + "." + now;
      api.ajax({
          url: url,
          method: 'post',
          report: true,
          headers: {
              "X-APICloud-AppId": appId,
              "X-APICloud-AppKey": appKey
          },
          data: {
              files: {
                  file: imageSrc
              }
          }
      }, function(ret, err) {
          //alert(JSON.stringify(ret));
          if (ret) {
              imgurl = ret.body.url;
              var imgId = ret.body.id;
              if (ret.status == 0) {
                  api.toast({
                      msg: '文件上传中' + ret.progress,
                      duration: 2000,
                      location: 'middle'
                  });
              } else {
                  uploadAjax(imgurl, content)
              }
              //alert( JSON.stringify( ret ) );
          } else {
              //alert( JSON.stringify( err ) );
          }
      });

  }

  function uploadAjax(imgurl, content) {
      var token = $api.getStorage('token');
      var body = {
          token: token,
          post_content: content,
          imgUrl: imgurl
      }
      api.ajax({
          url: 'http://www.mk517.com/webApi/publishPost.php',
          method: 'post',
          dataType: 'json',
          "headers": {
              "Content-Type": "application/json"
          },
          data: {
              body: body
          }
      }, function(ret, err) {
          if (ret) {
              if (ret.info == '发表成功') {
                  api.alert({
                      msg: '发表成功',
                  }, function(ret, err) {
                      if (ret) {
                          var index = '';
                          if (ret.buttonIndex == 1) {
                              api.closeFrame();
                              api.sendEvent({
                                  name: 'publish',
                                  extra: {
                                      index: 2
                                  }
                              });
                          }
                      } else {
                          //alert(JSON.stringify(err));
                      }
                  });

              } else {
                  api.alert({
                      msg: '发表失败！请重试'
                  }, function(ret, err) {});

              }
          } else {
              //  alert(JSON.stringify(err));
          }
      });
  }

}
